package com.sumainfor.wx.system.maket.service;

import com.sumainfor.common.entity.ParentDeptVo;
import com.sumainfor.common.entity.SysUserVo;
import com.sumainfor.common.util.Arith;
import com.sumainfor.common.util.ContextUtils;
import com.sumainfor.common.util.FieldCheckUtil;
import com.sumainfor.common.util.JsonResult;
import com.sumainfor.common.utlis.CompuUtils;
import com.sumainfor.common.utlis.ListMapMergeUtil;
import com.sumainfor.wx.system.feign.BaseFeignZuul;
import com.sumainfor.wx.system.feign.ZuulFeign.ZuulFeign;
import com.sumainfor.wx.system.maket.dao.LeaseConsultantMapper;
import org.apache.commons.collections.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

/**
 * <p>
 *
 * <p>
 *
 * @author: Mr.Wang
 * @create: 2019-08-15 14:36
 **/
@Service
public class LeaseConsultantService implements Serializable {

    Logger log= LoggerFactory.getLogger(LeaseConsultantService.class);

  @Autowired
    LeaseConsultantMapper leaseConsultantMapper;


  @Autowired
    BaseFeignZuul baseFeignZuul;


  /**
   *  本月公司成交排名
   * @author zhoucong
   * @data 2019/8/15
   * @Param
   * @return
   */
    public Map<String, Object> getMarketSalesClinchCom(Map<String,Object> params){
        Map<String,Object>result=new HashMap<String,Object>();
        //服务中心
        params.put("type",3);
        //获取当前用户下所有的服务中心
        List<Map<String,Object>>getDeptList= baseFeignZuul.getDeptList(params);
        List<String>DeptIdList=new ArrayList<>();
        if(getDeptList.size()>0){
            for(Map<String,Object> map:getDeptList){
                log.info("map-------------"+map);
                DeptIdList.add(map.get("DeptId").toString());
            }
        }
        //所有服务中心编号
        params.put("DeptIdList",DeptIdList);

        //所有人员
        JsonResult jsonResult=baseFeignZuul.getAllUserIdAndName();
        List<Map<String,Object>>getAllUserIdAndName=(List<Map<String,Object>>)jsonResult.getResult();

        //获取销售顾问本月公司成交
        List<Map<String,Object>>getMarketSalesClinchCom=leaseConsultantMapper.getMarketSalesClinchCom(params);
        //获取销售顾问本月公司带看记录
        List<Map<String,Object>>getMarketSalesBandCom=leaseConsultantMapper.getMarketSalesBandCom(params);


        List<Map<String,Object>>UserList= ListMapMergeUtil.ListMergeUtils(getAllUserIdAndName,getMarketSalesClinchCom,"userId");
        UserList=ListMapMergeUtil.ListMergeUtils(UserList,getMarketSalesBandCom,"userId");

        for(Map<String,Object> map:UserList){
            if(FieldCheckUtil.isMapFieldNotNull(map,new String[]{"heartCount"})&&
                    FieldCheckUtil.isMapFieldNotNull(map,new String[]{"bandCount"})){
                map.put("count", Arith.add(map.get("heartCount").toString(),map.get("bandCount").toString()).intValue());
            }else if(FieldCheckUtil.isMapFieldNotNull(map,new String[]{"heartCount"})){
                map.put("count",Arith.add(map.get("heartCount").toString(),"0").intValue());
                map.put("bandCount",0);
            }else if(FieldCheckUtil.isMapFieldNotNull(map,new String[]{"bandCount"})){
                map.put("count",Arith.add(map.get("bandCount").toString(),"0").intValue());
                map.put("heartCount",0);
            }else{
                map.put("count",0);
                map.put("heartCount",0);
                map.put("bandCount",0);
            }
        }


        //排序
        Collections.sort(UserList, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                Integer count1=Integer.valueOf(o1.get("count").toString());
                Integer count2=Integer.valueOf(o2.get("count").toString());
                return count2.compareTo(count1);
            }
        });

        String userId=null;
        //排名
        for(int i=0;i<UserList.size();i++){
            userId=UserList.get(i).get("userId").toString();
            if(userId.equals(ContextUtils.getUserId().toString())){
                result.put("order",i+1);
                break;
            }
        }


        //只取10位最大
        List<Map<String,Object>> resultList=new ArrayList<Map<String,Object>>();
        for(int i=0;i<UserList.size();i++){
            if(i<=9){
                resultList.add(UserList.get(i));
            }
        }

        //排序
        Collections.sort(resultList, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                Integer count1=Integer.valueOf(o1.get("count").toString());
                Integer count2=Integer.valueOf(o2.get("count").toString());
                return count1.compareTo(count2);
            }
        });

        //各人员名称
        List<String>chart1=new ArrayList<String>();
        //成交额
        List<String>chart2=new ArrayList<String>();
        //带看
        List<String>chart3=new ArrayList<String>();

        for(Map<String, Object> map:resultList){
            chart1.add(map.get("UserName").toString());
            chart2.add(map.get("heartCount").toString());
            chart3.add(map.get("bandCount").toString());
        }

        result.put("chart1",chart1);
        result.put("chart2",chart2);
        result.put("chart3",chart3);

        return result;

    }






    /**
     * 获取租赁顾问的本月成交排名（服务中心）
     * @author zhoucong
     * @data 2019/8/15
     * @Param
     * @return
     */
    public Map<String,Object> getMarketSalesHeart(Map<String,Object> params){

        Map<String,Object>result=new HashMap<String,Object>();

        SysUserVo userVo = ContextUtils.getSysUser();
        ParentDeptVo vo = null;
        for (int i = 0; i < userVo.getLength(); i++) {
            ParentDeptVo vo1 = userVo.getParentDeptVo();
            if (vo1.getType().equals(3)) {
                vo = vo1;
                break;
            }
        }
        //服务中心不为空
        if(vo!=null){
            params.put("deptId",vo.getDeptId());
        }

        //所有人员
        JsonResult jsonResult=baseFeignZuul.getAllUserIdAndName();
        List<Map<String,Object>>getAllUserIdAndName=(List<Map<String,Object>>)jsonResult.getResult();

        //获取租赁顾问的本月中心成交
        List<Map<String,Object>>getMarketSalesClinch=leaseConsultantMapper.getMarketSalesClinch(params);
        //获取租赁顾问的本月带看记录
        List<Map<String,Object>>getMarketSalesBand=leaseConsultantMapper.getMarketSalesBand(params);

        List<Map<String,Object>>UserList=ListMapMergeUtil.ListMergeUtils(getAllUserIdAndName,getMarketSalesClinch,"userId");
        UserList=ListMapMergeUtil.ListMergeUtils(UserList,getMarketSalesBand,"userId");


        for(Map<String,Object> map:UserList){
            if(FieldCheckUtil.isMapFieldNotNull(map,new String[]{"heartCount"})&&
                    FieldCheckUtil.isMapFieldNotNull(map,new String[]{"bandCount"})){
                map.put("count",Arith.add(map.get("heartCount").toString(),map.get("bandCount").toString()).intValue());
            }else if(FieldCheckUtil.isMapFieldNotNull(map,new String[]{"heartCount"})){
                map.put("count",Arith.add(map.get("heartCount").toString(),"0").intValue());
                map.put("bandCount",0);
            }else if(FieldCheckUtil.isMapFieldNotNull(map,new String[]{"bandCount"})){
                map.put("count",Arith.add(map.get("bandCount").toString(),"0").intValue());
                map.put("heartCount",0);
            }else{
                map.put("count",0);
                map.put("heartCount",0);
                map.put("bandCount",0);
            }
        }

        //排序
        Collections.sort(UserList, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                Integer count1=Integer.valueOf(o1.get("count").toString());
                Integer count2=Integer.valueOf(o2.get("count").toString());
                return count2.compareTo(count1);
            }
        });

        String userId=null;
        //排名
        for(int i=0;i<UserList.size();i++){
            userId=UserList.get(i).get("userId").toString();
            log.info("UserList----"+UserList.get(i));
            log.info("ContextUtils.getUserId()----"+ContextUtils.getUserId());
            if(userId.equals(ContextUtils.getUserId().toString())){
                result.put("order",i+1);
                break;
            }
        }

        //只取10位最大
        List<Map<String,Object>>resultList=new ArrayList<Map<String,Object>>();
        for(int i=0;i<UserList.size();i++){
            if(i<=9){
                resultList.add(UserList.get(i));
            }
        }

        //排序
        Collections.sort(resultList, new Comparator<Map<String, Object>>() {
            @Override
            public int compare(Map<String, Object> o1, Map<String, Object> o2) {
                Integer count1=Integer.valueOf(o1.get("count").toString());
                Integer count2=Integer.valueOf(o2.get("count").toString());
                return count1.compareTo(count2);
            }
        });

        //各人员名称
        List<String>chart1=new ArrayList<String>();
        //成交额
        List<String>chart2=new ArrayList<String>();
        //带看
        List<String>chart3=new ArrayList<String>();

        for(Map<String, Object> map:resultList){
            chart1.add(map.get("UserName").toString());
            chart2.add(map.get("heartCount").toString());
            chart3.add(map.get("bandCount").toString());
        }

        result.put("chart1",chart1);
        result.put("chart2",chart2);
        result.put("chart3",chart3);
        return result;

    }



    /**
     * 今日已签约，未来需带看，在跟租客
     * @author zhoucong
     * @data 2019/8/15
     * @Param
     * @return
     */
    public JsonResult getMarketSalesStatus(Map<String,Object> params){
        JsonResult result = new JsonResult();
        return result.put(leaseConsultantMapper.getMarketSalesStatus(params));

    }


    /**
     * 今日已带看，本月已带看，本月已签约 签约成交率
     * @author zhoucong
     * @data 2019/8/15
     * @Param
     * @return
     */
    public JsonResult getStatus(Map<String,Object> params){
        JsonResult result = new JsonResult();
        int a = 2;
        Map<String,Object> zczsMap = leaseConsultantMapper.getStatus(params);
        double zczs = 0;
        if(MapUtils.getDouble(zczsMap,"zcState5")>0) {
             zczs = CompuUtils.div(MapUtils.getDouble(zczsMap, "zcState4"), MapUtils.getDouble(zczsMap, "zcState5"), a);
        }
        int  zcState6 = (int)CompuUtils.mul(zczs,100);
        zczsMap.put("zcState6",zcState6);
        return result.put(zczsMap);

            }



}
